#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#	For Korean to Enlish conversion
#	Digital Meida Lab, ICU, Korea 
#	Youmin Kim (ym_kim@icu.ac.kr)

import hangul
import tagpot, MySQLdb, string
from numpy import *
cursor = tagpot.dbConnect()

def getPhone(s):
	phone = {
		u'\u3131':u'g',	# jaum
		u'\u3132':u'gg', 
		u'\u3133':u'gs', 
		u'\u3134':u'n', 
		u'\u3135':u'nj', 
		u'\u3136':u'nh',
		u'\u3137':u'd', 
		u'\u3138':u'dd', 
		u'\u3139':u'l', 
		u'\u313a':u'lg',
		u'\u313b':u'lm',
		u'\u313c':u'lb',
		u'\u313d':u'ls',
		u'\u313e':u'lt',
		u'\u313f':u'lp',
		u'\u3140':u'lh',
		u'\u3141':u'm', 
		u'\u3142':u'b',
		u'\u3143':u'bb', 
		u'\u3144':u'bs',
		u'\u3145':u's', 
		u'\u3146':u'ss',
		u'\u3147':u'ng',
		u'\u3148':u'j',
		u'\u3149':u'jj',
		u'\u314a':u's',
		u'\u314b':u'g',
		u'\u314c':u't', 
		u'\u314d':u'p', 
		u'\u314e':u'h',

		u'\u314f':u'a',	# moum 
		u'\u3150':u'e', 
		u'\u3151':u'ya', 
		u'\u3152':u'ye', 
		u'\u3153':u'u', 
		u'\u3154':u'e',
		u'\u3155':u'ye', 
		u'\u3156':u'ye', 
		u'\u3157':u'o', 
		u'\u3158':u'wa', 
		u'\u3159':u'we', 
		u'\u315a':u'oe',
		u'\u315b':u'yo', 
		u'\u315c':u'u', 
		u'\u315d':u'we',
		u'\u315e':u'we', 
		u'\u315f':u'wi', 
		u'\u3160':u'u',
		u'\u3161':u'eu', 
		u'\u3162':u'i', 
		u'\u3163':u'i'
	}
	phone_en = {
		
		u'k':u'g',
		u'v':u'b',
		u'c':u's',
		u'x':u's',
		u'z':u'j',
		u'f':u'p',
		u'r':u'l',
	}


	re = [];
	for i in s:
		if not hangul.ishangul(i):
			
			# ﾿ﾵﾹﾮ ￀ￚ￀ﾽ ﾴￜﾼ￸￈ﾭ
			if phone_en.has_key(i):
				i = phone_en[i]
			re.append(i)
			re.append(i)
			continue
		t = hangul.split(i)
		c = 0
		for g in t:
			c = c + 1
			if g == u'':
				continue

			# 초성에 이응 표기 시에는 그냥 지나감
			if c == 1 and phone[g] == 'ng':
				continue
			# 'ㅡ' 표기시 그냥 지나감
			if phone[g] == u'eu':
				continue
			else: 
				if phone.has_key(g):
					ph = phone[g]
					re.append(ph)
	return re

def levenDistance(s,t):
	m = len(s)+1
	n = len(t)+1
	s.insert(0,'')
	t.insert(0,'')
	
	d = zeros((m, n))
	for i in range(0, m):
		d[i,0] = i
	for i in range(0,n):
		d[0,i] = i;
		
	cost = 0
	for i in range(1, m):
		for j in range(1, n):
			
			# TODO: 비슷한 모음배열에 대한 가중치에 대해 생각해보기
			if s[i].lower() == t[j].lower():
				cost = 0
			else:
				cost = 1
			d[i,j] = min ([d[i-1,j]+1,d[i, j-1]+1, d[i-1, j-1]+cost])
	return d[m-1, n-1]
				
			  

if __name__ == '__main__':
	q = "select target,candidate, freq from lang";
	cursor.execute(q)
	source = cursor.fetchall()
	for r in source:
		target=u""
		candidate=u""
		freq= r[2]
		
		target = string.replace(unicode(r[0], 'utf8'), u" ", u"")
		candidate = string.replace(unicode(r[1], 'utf8'), u" ", u"")
		tar = []
		can = []
		for t in target:
			tar.append(t)
		for c in candidate:
			can.append(c)
		
		dt = []
		dc = []
		dt, dc = getPhone(target), getPhone(candidate)
		
		# TODO: relatioship b/w target, candidate (ex.ko-en, ko-ko) 
		err = levenDistance(dt, dc)/(len(dt)+len(dc))*2
		
		"""
		score = freq/30/(err+0.01)
		if err < 0.6 and freq > 20:
			print target, candidate, score
		"""
		
		q = "update lang set phone = %s where target ='%s' and candidate = '%s'" %(err, r[0], r[1])
		cursor.execute(q)
		
		
		